package com.frxuexi.park2209.user

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.base.entity.UserBean
import com.frxuexi.base.mvvm.BaseActivity
import com.frxuexi.park2209.R
import com.frxuexi.park2209.databinding.ActivityRegisterBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@AndroidEntryPoint
class RegisterActivity : BaseActivity<ActivityRegisterBinding,LoginViewModel>() {
    override fun initData() {

        lifecycleScope.launch{
            vm.registerSuc.collect{
                if(it.toInt() >0){
                    Log.i("===",it.toString())
                    ToastUtils.showLong("注册成功")
                    finish()
                }
            }
        }
    }

    override fun initView() {

        binding.btnRegister.setOnClickListener {
            val name = binding.etName.text.toString()
            val pass = binding.etPass.text.toString()
            val pass2 = binding.etPass2.text.toString()
            if(name.isNullOrEmpty() || pass.isNullOrEmpty()|| pass2.isNullOrEmpty()){
                ToastUtils.showLong("输入内容不能为空")
                return@setOnClickListener
            }

            if(!name.matches(Regex("[0-9A-z]{2,10}"))){
                ToastUtils.showLong("用户名不符合规则")
                return@setOnClickListener
            }

            if(pass!=pass2){
                ToastUtils.showLong("两次密码不一致")
                return@setOnClickListener
            }

            vm.register(UserBean(uName = name, uPwd = pass.toInt(), time = System.currentTimeMillis()))
        }
    }

}